package com.fish.web.controller;

import com.fish.common.annotation.OperationLog;
import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.model.dto.OrganizationDTO;
import com.fish.model.dto.UserOrganizationDTO;
import com.fish.model.vo.OrganizationVO;
import com.fish.model.vo.UserOrganizationVO;
import com.fish.service.OrganizationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组织Controller
 */
@Tag(name = "组织管理", description = "组织架构和成员管理相关接口")
@Slf4j
@RestController
@RequestMapping("/api/organization")
public class OrganizationController {
    
    @Autowired
    private OrganizationService organizationService;
    
    /**
     * 获取组织树
     */
    @GetMapping("/tree")
    public Result<List<OrganizationVO>> getOrganizationTree() {
        log.info("获取组织树");
        List<OrganizationVO> tree = organizationService.getOrganizationTree();
        return Result.success(tree);
    }
    
    /**
     * 根据父ID获取子组织列表
     */
    @GetMapping("/children/{parentId}")
    public Result<List<OrganizationVO>> getOrganizationsByParentId(@PathVariable Long parentId) {
        log.info("获取子组织列表，父组织ID：{}", parentId);
        List<OrganizationVO> list = organizationService.getOrganizationsByParentId(parentId);
        return Result.success(list);
    }
    
    /**
     * 根据ID获取组织详情
     */
    @GetMapping("/{id}")
    public Result<OrganizationVO> getOrganizationById(@PathVariable Long id) {
        log.info("获取组织详情，组织ID：{}", id);
        OrganizationVO org = organizationService.getOrganizationById(id);
        return Result.success(org);
    }
    
    /**
     * 创建组织
     */
    @PostMapping("/create")
    @RequiresPermission("system:organization:add")
    @OperationLog("创建组织")
    public Result<OrganizationVO> createOrganization(@RequestBody OrganizationDTO dto) {
        log.info("创建组织，组织名称：{}", dto.getOrgName());
        OrganizationVO org = organizationService.createOrganization(dto);
        return Result.success("创建成功", org);
    }
    
    /**
     * 更新组织
     */
    @PutMapping("/update")
    @RequiresPermission("system:organization:edit")
    @OperationLog("更新组织")
    public Result<OrganizationVO> updateOrganization(@RequestBody OrganizationDTO dto) {
        log.info("更新组织，组织ID：{}", dto.getId());
        OrganizationVO org = organizationService.updateOrganization(dto);
        return Result.success("更新成功", org);
    }
    
    /**
     * 删除组织
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermission("system:organization:delete")
    @OperationLog("删除组织")
    public Result<Void> deleteOrganization(@PathVariable Long id) {
        log.info("删除组织，组织ID：{}", id);
        organizationService.deleteOrganization(id);
        return Result.success("删除成功", null);
    }
    
    /**
     * 获取组织成员列表
     */
    @GetMapping("/members/{orgId}")
    @RequiresPermission("system:organization:members")
    public Result<List<UserOrganizationVO>> getOrganizationMembers(@PathVariable Long orgId) {
        log.info("获取组织成员列表，组织ID：{}", orgId);
        List<UserOrganizationVO> members = organizationService.getOrganizationMembers(orgId);
        return Result.success(members);
    }
    
    /**
     * 添加组织成员
     */
    @PostMapping("/member/add")
    @RequiresPermission("system:organization:addMember")
    @OperationLog("添加组织成员")
    public Result<Void> addOrganizationMember(@RequestBody UserOrganizationDTO dto) {
        log.info("添加组织成员，用户ID：{}，组织ID：{}", dto.getUserId(), dto.getOrgId());
        organizationService.addOrganizationMember(dto);
        return Result.success("添加成功", null);
    }
    
    /**
     * 移除组织成员
     */
    @DeleteMapping("/member/remove/{userId}/{orgId}")
    @RequiresPermission("system:organization:removeMember")
    @OperationLog("移除组织成员")
    public Result<Void> removeOrganizationMember(@PathVariable Long userId, @PathVariable Long orgId) {
        log.info("移除组织成员，用户ID：{}，组织ID：{}", userId, orgId);
        organizationService.removeOrganizationMember(userId, orgId);
        return Result.success("移除成功", null);
    }
    
    /**
     * 更新用户组织关系
     */
    @PutMapping("/member/update")
    @RequiresPermission("system:organization:editMember")
    @OperationLog("编辑组织成员")
    public Result<Void> updateUserOrganization(@RequestBody UserOrganizationDTO dto) {
        log.info("编辑组织成员，用户ID：{}，组织ID：{}", dto.getUserId(), dto.getOrgId());
        organizationService.updateUserOrganization(dto);
        return Result.success("更新成功", null);
    }
    
    /**
     * 获取用户所在的所有组织
     */
    @GetMapping("/user/{userId}")
    public Result<List<OrganizationVO>> getUserOrganizations(@PathVariable Long userId) {
        log.info("获取用户组织列表，用户ID：{}", userId);
        List<OrganizationVO> orgs = organizationService.getUserOrganizations(userId);
        return Result.success(orgs);
    }
    
    /**
     * 获取组织下所有用户ID
     */
    @GetMapping("/users/{orgId}")
    public Result<List<Long>> getUserIdsByOrgId(
            @PathVariable Long orgId,
            @RequestParam(defaultValue = "false") boolean includeChildren) {
        log.info("获取组织用户ID列表，组织ID：{}，包含子组织：{}", orgId, includeChildren);
        List<Long> userIds = organizationService.getUserIdsByOrgId(orgId, includeChildren);
        return Result.success(userIds);
    }
}

